package com.bootdo.wechat.controller;

import java.util.List;
import java.util.Map;

import com.bootdo.wechat.constant.WechatEnum;
import com.bootdo.wechat.domain.MpConfigDO;
import com.bootdo.wechat.service.MpConfigService;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.WxMpUserService;
import me.chanjar.weixin.mp.bean.result.WxMpUserList;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.wechat.domain.MpFansDO;
import com.bootdo.wechat.service.MpFansService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;

/**
 *
 *
 * @author songjw
 * @email songjw1993@126.com
 * @date 2018-08-03 12:19:13
 */

@Controller
@RequestMapping("/wechat/mpFans")
public class MpFansController extends MpBaseController {
	@Autowired
	private MpFansService mpFansService;
	@Autowired
	private MpConfigService mpConfigService;

	@GetMapping("/{id}")
	@RequiresPermissions("wechat:mpFans:mpFans")
	String MpFans(@PathVariable("id") String id,Model model){
		model.addAttribute("id",id);
		return "wechat/mpFans/mpFans";
	}

	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("wechat:mpFans:mpFans")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
		Query query = new Query(params);
		List<MpFansDO> mpFansList = mpFansService.list(query);
		int total = mpFansService.count(query);
		PageUtils pageUtils = new PageUtils(mpFansList, total);
		return pageUtils;
	}

	/*@GetMapping("/add")
	@RequiresPermissions("wechat:mpFans:add")
	String add(){
		return "wechat/mpFans/add";
	}*/

	@GetMapping("/edit/{id}")
	@RequiresPermissions("wechat:mpFans:edit")
	String edit(@PathVariable("id") String id,Model model){
		MpFansDO mpFans = mpFansService.get(id);
		model.addAttribute("mpFans", mpFans);
		return "wechat/mpFans/edit";
	}

	/**
	 * 拉取微信端粉丝数据
	 */
	@ResponseBody
	@GetMapping("/pullFans/{mpId}")
	@RequiresPermissions("wechat:mpFans:pull")
	public R pullFans(@PathVariable("mpId")String id ){
		WxMpService wxService = getWxService(id);
		List<String> openids = null;
		WxMpUserService userService = wxService.getUserService();
		WxMpUserList wxMpUserList = null;
		int failNum = 0;
		try {
			wxMpUserList =userService.userList(null);
			openids = wxMpUserList.getOpenids();
			int i = mpFansService.pullFans(id, openids, userService);
			failNum+=i;
			int total =(int) wxMpUserList.getTotal();
			while (total>10000){
				total-=10000;
				wxMpUserList =userService.userList(wxMpUserList.getNextOpenid());
				openids = wxMpUserList.getOpenids();
				int a = mpFansService.pullFans(id, openids, userService);
				failNum+=a;
			}

		}catch (WxErrorException wx){
			return R.error();
		}
		return  R.ok("本次拉取数量："+wxMpUserList.getTotal()+",失败数量为："+failNum);
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("wechat:mpFans:edit")
	public R update( MpFansDO mpFans){
		mpFansService.update(mpFans);
		return R.ok();
	}

	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("wechat:mpFans:remove")
	public R remove( String id){
		if(mpFansService.remove(id)>0){
			return R.ok();
		}
		return R.error();
	}

	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("wechat:mpFans:batchRemove")
	public R remove(@RequestParam("ids[]") String[] ids){
		mpFansService.batchRemove(ids);
		return R.ok();
	}

}
